home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 104_01 / stdlib.asm < prev    next >
Assembly Source File  |  1980-01-01  |  26KB  |  996 lines

  1.     title    small c i/o package
  2.     extrn    bios
  3. bdos    equ    5        ;address on entry address for bdos
  4. cpmcmd  equ    80h        ;address of cpm command line
  5. true    equ    1        ;value for true
  6. false    equ    0        ;value for false        
  7. ;
  8. ;    toupper
  9. ;
  10. ;    function: to shift lower case character to upper
  11. ;
  12. ;    calling format from "c"
  13. ;    toupper(char);
  14. ;
  15. toupper:csect
  16.     mov    a,l        ;get character to test
  17.     cpi    61h        ;check to see if less then 'a'
  18.     rc            ;yes return to caller
  19.     cpi    7bh        ;check to see if greater then 'z'
  20.     rnc            ;yes..return to caller
  21.     ani    5fh        ;mask off lowwer case bit
  22.     mov    l,a        ;put it in reg for return
  23.     ret            ;return to caller
  24. ;
  25. ;    tolowwer
  26. ;
  27. ;    function: to convert character to lowwer case
  28. ;
  29. ;    calling format from "c"
  30. ;    tolowwer(char)
  31. ;
  32. tolowwer:csect
  33.     mov    a,l        ;get character to shift to lowwer case
  34.     cpi    'A'        ;see if less then 'A'
  35.     rc            ;yes...return to caller
  36.     cpi    'Z'+1        ;see if greater then 'Z'
  37.     rnc            ;yes...return to caller
  38.     ani    0bfh        ;remove upper case bit
  39.     mov    l,a        ;put chracter back in place
  40.     ret
  41. ;
  42. ;    isalpha
  43. ;
  44. ;    function check to see if alpha character a-z or A-Z only
  45. ;
  46. ;    calling format from "c"
  47. ;    isalpha(char)
  48. ;
  49. isalpha:csect
  50.     push    h        ;save calling arg for later
  51.     call    isupper        ;check to see if upper case
  52.     xchg            ;put answer in de
  53.     pop    h        ;get character to test back
  54.     call    islowwer    ;check to see if lowwer case
  55.     dad    d        ;add isupper to is lowwer answer
  56.     ret
  57.  
  58. ;
  59. ;    isupper
  60. ;
  61. ;    function: to check to see if character is upper case
  62. ;
  63. ;    calling format in "c"
  64. ;    isupper(char) 
  65. ;
  66. isupper:csect
  67.     mov    a,l        ;get character to check
  68.     cpi    'A'        ;see if less then upper case a
  69.     jc    isupper1    ;not upper case
  70.     cpi    'Z'+1        ;check to see if less then 
  71.     jnc    isupper1    ;no upper case
  72.     lxi    h,true        ;yes it is upper case
  73.     ret
  74. isupper1:
  75.     lxi    h,false        ;not upper case
  76.     ret
  77. ;
  78. ;    islowwer
  79. ;
  80. ;    function: to check to see if character is lowwer case
  81. ;
  82. ;    calling format from "c"
  83. ;    islowwer(char)
  84. ;
  85. islowwer:csect
  86.     mov    a,l        ;get character to check
  87.     cpi    'a'        ;see if less then 'a'
  88.     jc    islowwer1    ;yes...not upper case
  89.     cpi    'z'+1        ;is it greater then 'z'
  90.     jnc    islowwer1    ;yes...not lower case
  91.     lxi    h,true        ;return true for lowwer case
  92.     ret
  93. islowwer1:
  94.     lxi    h,false        ;return false for anything but lowwer case
  95.     ret
  96. ;
  97. ;    isdigit
  98. ;
  99. ;    function: to check to see if character is digit 0-9 only
  100. ;
  101. ;    calling format from "c"
  102. ;    isdigit(char)
  103. ;
  104. isdigit:csect
  105.     mov    a,l        ;get character to test
  106.     cpi    '0'        ;see if less then a zero
  107.     jc    isdigit1    ;yes...not a vaild digit
  108.     cpi    '9'+1        ;is it greater than nine
  109.     jnc    isdigit1    ;yes...not a vaild digit
  110.     lxi    h,true        ;is a vaild digit
  111.     ret
  112. isdigit1:
  113.     lxi    h,false        ;not a vaild digit
  114.     ret
  115. ;
  116. ;    isspace
  117. ;
  118. ;    function: to check to see if white space tab or blank
  119. ;
  120. ;    calling format from "c"
  121. ;    isspace(char)
  122. ;
  123. isspace:csect
  124.     mov    a,l        ;get character to check
  125.     lxi    h,true        ;assume true
  126.     cpi    ' '        ;check for a space
  127.     rz
  128.     cpi    9        ;check for a tab
  129.     rz
  130.     lxi    h,false
  131.     ret
  132. ;
  133. ;    strlen
  134. ;
  135. ;    function: to get the lenght of a string
  136. ;
  137. ;    calling format in "c"
  138. ;    len=strlen(string);
  139. ;
  140. strlen:    csect
  141.     xchg            ;put address of string in de
  142.     lxi    h,0        ;make string of zero lenght
  143. strlen1:
  144.     ldax    d        ;get character from string
  145.     ora    a        ;check to see if end of string
  146.     rz            ;yes return to caller
  147.     inx    h        ;add 1 to string lenght
  148.     inx    d        ;move to next character
  149.     jmp    strlen1        ;loop till end of string found
  150. ;
  151. ;    strcpy
  152. ;
  153. ;    function: to copy first second string to first string
  154. ;
  155. ;    calling format from "c"
  156. ;    strcpy(object,source);
  157. ;
  158. strcpy:    csect
  159.     pop    b        ;get return address from stack
  160.     pop    d        ;get source address
  161.     pop    h        ;get object address
  162.     push    h        ;restore machine stack
  163.     push    d
  164.     push    b
  165.     push    h
  166. strcpy1:
  167.     ldax    d        ;get address of source  character
  168.     mov    m,a        ;store character in object string
  169.     ora    a        ;check to see if end of string
  170.     jz    strcpy2        ;end of string return to caller
  171.     inx    h
  172.     inx    d        ;move pointer to next byte
  173.     jmp    strcpy1        ;loop till done with copy
  174. strcpy2:
  175.     pop    h        ;return address of object string
  176.     ret
  177. ;
  178. ;    strcat
  179. ;
  180. ;    function: to put string2 at then end of string1 and return address of 
  181. ;                string 1
  182. ;
  183. ;    calling format in "c"
  184. ;    strcat(string1,string2);
  185. ;
  186. strcat:    csect
  187.     pop    b        ;get return address
  188.     pop    d        ;get address of string 2
  189.     pop    h        ;get address of string 1
  190.     push    h        ;retore machine stack
  191.     push    d
  192.     push    b
  193.     push    h        ;save address of source string
  194. strcat1:
  195.     mov    a,m        ;get character from source buffer
  196.     ora    a        ;check to see if zero
  197.     jz    strcat2        ;yes end of string found
  198.     inx    h        ;move pointer to next byte
  199.     jmp    strcat1        ;loop till end of string found
  200. strcat2:
  201.     ldax    d        ;get character from string 2
  202.     mov    m,a        ;save character in string1
  203.     ora    a        ;check to see if end of buffer
  204.     jz    strcat3        ;yes return to caller
  205.     inx    h        ;move object pointer up 1
  206.     inx    d        ;move source pointer up 1
  207.     jmp    strcat2
  208. strcat3:
  209.     pop    h        ;get address of string1
  210.     ret
  211. ;    strpos
  212. ;
  213. ;    function: to look for string 2 in string 1
  214. ;
  215. ;    calling format in "c"
  216. ;    strpos(string1,string2);
  217. ;
  218. strpos:    csect
  219.     call    argload        ;get args load into read be=1 de=2 hl=3
  220.     push    b
  221.     pop    h
  222.     lxi    b,1        ;de=string2, hl=string1 bc=0
  223.     xchg
  224. strpos1:
  225.     ldax    d        ;get character to check
  226.     ora    a        ;check to see if end of string
  227.     jz    strpos5        ;end of string1 string 2 not found in string1
  228.     cmp    m        ;is it equal to string2 character 1
  229.     jz    strpos2        ;yes...
  230.     inx    d        ;no add 1 to string 1 pointer
  231.     inx    b        ;add 1 to offset pointer
  232.     jmp    strpos1        ;loop till end of string1 or string2 found
  233. strpos2:
  234.     push    h        ;save strating address of string 21
  235.     push    d        ;save current address of string 1
  236. strpos3:
  237.     inx    d
  238.     inx    h
  239.     mov    a,m        ;get character from string 2
  240.     ora    a        ;set machine flags
  241.     jz    strpos4        ;end of string2 found must be a mathc
  242.     ldax    d        ;get arg1
  243.     cmp    m        ;is this character a match
  244.     jz    strpos3 
  245.     pop    d        ;restore pointer for string1 and string 2
  246.     pop    h
  247.     inx    d        ;add 1 to string pointer
  248.     inx    b        ;add 1 to offset in string1
  249.     jmp    strpos1        ;loop till end of string 1
  250. strpos4:
  251.     mov    l,c        ;string found return offset for caller
  252.     mov    h,b
  253.     pop    b
  254.     pop    b
  255.     ret
  256. strpos5:
  257.     lxi    h,0        ;string not found
  258.     ret
  259. ;
  260. ;    setmem
  261. ;
  262. ;    function: to fill a block of memory with a given constant
  263. ;
  264. ;    calling format in "c"
  265. ;    setmem(address,count,constant);
  266. ;
  267. setmem:    csect
  268.     pop    b        ;get return address
  269.     pop    h        ;get constant
  270.     mov    a,l        ;put constant in a
  271.     pop    b        ;get count
  272.     pop    d        ;get address
  273.     lxi    h,8        ;offset in to put stack back
  274.     dad    sp
  275.     sphl            ;restore stack pointer
  276.     mov    l,a        ;place to save constant
  277. setmem1:
  278.     mov    a,b        ;check to see if count is zero
  279.     ora    c
  280.     rz            ;all done with move
  281.     mov    a,l        ;get constant to store
  282.     stax    d        ;set memory loaction
  283.     inx    d        ;move pointer to next pointer
  284.     dcx    b        ;subtract 1 from count
  285.     jmp    setmem1        ;loop till all bytes set
  286. ;
  287. ;    movmem
  288. ;
  289. ;    function: to move source address to object address for count bytes
  290. ;
  291. ;    calling format from  "C"
  292. ;    movmem(source,object,count);
  293. ;
  294. movmem:    csect
  295.     call    argload        ;get args bc=1, de=2, hl=3
  296.     push    b        ;switch args so that 
  297.     push    d        ;bc=count
  298.     push    h        ;de=object
  299.     pop    b        ;hl=source
  300.     pop    h
  301.     pop    d
  302.     mov    a,b        ;check to see if count = 0
  303.     ora    c
  304.     rz            ;zero return to caller
  305.     call    movmemht    ;check to see if source<dest
  306.     jc    movmemt        ;move tial first
  307.     mvi    a,2        ;check to see if z80
  308.     inr    a
  309.     jpe    movmemf8    ;8080 do a byte by byte move
  310.     dw    0b0edh
  311.     ret
  312. movmemf8:
  313.     mov    a,m        ;get source byte
  314.     stax    d        ;save byte in object buffer
  315.     inx    h        ;move object pointer up 1
  316.     inx    d        ;move source pointer up 1 byte
  317.     dcx    b        ;subtract 1 from count
  318.     mov    a,b        ;see if done
  319.     ora    c
  320.     jnz    movmemf8    ;no keep on looping
  321.     ret
  322. movmemt:
  323.     dcx    b        ;tail first. compute new source
  324.     dad    b        ;and destination address
  325.     xchg
  326.     dad    b
  327.     xchg
  328.     mvi    a,2        ;check to see if z80
  329.     inr    a
  330.     jpe    movmemt8    ;8080 do a byte by byte move
  331.     dw    0b8edh
  332.     ret
  333. movmemt8:
  334.     mov    a,m        ;get source byte
  335.     stax    d        ;save object byte
  336.     dcx    h        ;move to next byte of source buffer
  337.     dcx    d        ;move to next byte of object buffer
  338.     dcx    b        ;subtract 1 from count
  339.     mov    a,b        ;cehck to see if all done
  340.     ora    c
  341.     jnz    movmemt8    ;no keep on looping
  342.     ret
  343. movmemht:
  344.     mov    a,h
  345.     cmp    d
  346.     rnz
  347.     mov    a,l
  348.     cmp    e
  349.     ret
  350. ;
  351. ;    outp
  352. ;
  353. ;    function: to output 1 byte t